﻿using UnityEngine;

namespace LuaInterface
{
    /// <summary>
    /// 可以用多个相同的，建议使用这个
    /// </summary>
    public class LuaComponent : MonoBehaviour
    {
        public AssetsRef[] assetsRef = null;

        public string luaPath = null;

        internal string _name;

        internal LuaTable _table;
        public LuaTable luaTable
        {
            get
            {
                return _table;
            }
        }

        public static LuaTable Get(GameObject go, LuaTable table)
        {
            LuaComponent[] cmps = go.GetComponents(typeof(LuaComponent)) as LuaComponent[];
            foreach (LuaComponent cmp in cmps)
            {
                string mat1 = table.ToString();
                string mat2 = cmp._table.GetMetaTable().ToString();
                if (mat1 == mat2)
                    return cmp._table;
            }
            return null;
        }

        void Awake()
        {
            LuaManager _instance = LuaManager.Instance;
            if (!string.IsNullOrEmpty(luaPath))
            {
                _name = System.IO.Path.GetFileName(luaPath);

                // LuaManager.Instance.DoFile(luaPath);
                // _table = LuaManager.Instance.Lua.GetTable(_name);
                _table = LuaManager.Instance.RequireFile(luaPath);

                if (_table != null)
                {
                    LuaFunction func = _table.GetLuaFunction("New");
                    if (func != null)
                    {
                        _table = func.Invoke<LuaTable>();
                        func.Dispose();
                        func = null;
                    }
                    else
                    {
                        _table = null;
                    }

                    if (_table != null)
                    {
                        _table["gameObject"] = gameObject;
                        _table["transform"] = transform;
                        if (assetsRef != null)
                        {
                            foreach (var ass in assetsRef)
                            {
                                _table[ass.name] = ass;
                            }
                        }
                        func = _table.GetLuaFunction("Awake");
                        if (func != null)
                        {
                            func.Call(_table);
                            func.Dispose();
                            func = null;
                        }
                    }
                }
            }
        }

        void OnEnable()
        {
            if (!string.IsNullOrEmpty(_name) && _table != null)
            {
                LuaFunction func = _table.GetLuaFunction("OnEnable");
                if (func != null)
                {
                    func.Call(_table);
                    func.Dispose();
                    func = null;
                }
            }
        }

        // Use this for initialization
        void Start()
        {
            if (_table != null)
            {
                LuaFunction func = _table.GetLuaFunction("Start");
                if (func != null)
                {
                    func.Call(_table);
                    func.Dispose();
                    func = null;
                }
            }
        }


        void OnDisable()
        {
            if (!LuaManager.IsInited || _table == null) return;

            LuaFunction func = _table.GetLuaFunction("OnDisable");
            if (func != null)
            {
                func.Call(_table);
                func.Dispose();
                func = null;
            }
        }

        public void CallLuaFun(string luaFunc)
        {
            if (!LuaManager.IsInited || _table == null) return;

            LuaFunction func = _table.GetLuaFunction(luaFunc);
            if (func != null)
            {
                func.Call(_table);
                func.Dispose();
                func = null;
            }
        }

        void OnDestroy()
        {
            if (!LuaManager.IsInited) return;

            if (_table != null)
            {
                LuaFunction func = _table.GetLuaFunction("OnDestroy");
                if (func != null)
                {
                    func.Call(_table);
                    func.Dispose();
                    func = null;
                }
                _table.Dispose();
            }
        }

    }
}